<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="140px">
      <el-form-item label="企业名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属辖区" prop="xqid">
        <el-cascader :props="props" @change="changeQueryXqid"></el-cascader>
      </el-form-item>
      <el-form-item label="检查状态" prop="status">
        <el-select v-model="queryParams.status" clearable>
          <el-option
            v-for="(dict, index) in queryStatus"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-popover
          placement="right-start"
          width="500"
          trigger="click">
          <el-form :inline="true" :model="formExport" class="demo-form-inline">
            <el-form-item label="选择月份">
              <el-date-picker
                v-model="formExport.month"
                type="month"
                placeholder="选择月"
                value-format="yyyy-MM"
                @change="changeMonth"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleExport">导出</el-button>
            </el-form-item>
          </el-form>
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            slot="reference"
            v-hasPermi="['system:store:export']"
          >按月导出</el-button>
        </el-popover>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="companyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所属辖区" align="center" prop="deptName" />
      <el-table-column label="企业名称" align="center" prop="name" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="负责人" align="center" prop="fzr" />
      <el-table-column label="安全人员" align="center" prop="safeMember" />
      <el-table-column label="负责人联系方式" align="center" prop="fzrPhone" />
      <el-table-column label="安全人员联系方式" align="center" prop="safePhone" />
      <el-table-column label="重点区域" align="center" prop="imptArea" />
      <el-table-column label="检查状态" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag effect="plain" :type="dictIsCheck[scope.row.isCheck].type">{{ dictIsCheck[scope.row.isCheck].text }}</el-tag>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getRecordCom, companyExport } from "@/api/system/patrol";
import { getdeptByparentid } from '@/api/system/dept'
import {getToken} from "@/utils/auth";
import store from "@/store";
import { getLastDay } from '@/utils'

export default {
  name: "Company",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业基础信息表格数据
      companyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        name: null,
        address: null,
        fzr: null,
        safeMember: null,
        fzrPhone: null,
        safePhone: null,
        imptArea: null,
        creater: null,
        operter: null,
        operTime: null,
        status: null
      },
      props: {
        checkStrictly: true,
        lazy: true,
        lazyLoad (node, resolve) {
          const params = {
            parentid: ['admin', 'adm'].filter(item => store.getters.roles.includes(item)).length ? 200 : store.getters.deptId,
            deptType: '0'
          }
          console.log('node', node)
          params.parentid = node.value || (['admin', 'adm'].filter(item => store.getters.roles.includes(item)).length ? 200 : store.getters.deptId)
          const { level } = node;
          getdeptByparentid(params).then(res => {
            const nodes = res.data.map(item => {
              return {
                value: item.deptId,
                label: item.deptName,
                leaf: level >= 1
              }
            })
            resolve(nodes)
          })
        }
      },
      dictIsCheck: {
        false: {
          text: '未检查',
          type: 'danger',
          value: 0
        },
        true: {
          text: '已检查',
          type: 'success',
          value: 1
        },
        null: {
          text: '未知',
          type: 'info'
        }
      },
      queryStatus: [
        {
          label: '未检查',
          value: 0
        },
        {
          label: '已检查',
          value: 1
        }
      ],
      formExport: {
        month: ''
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询企业基础信息列表 */
    getList() {
      this.loading = true;
      getRecordCom(this.queryParams).then(response => {
        this.companyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        name: null,
        address: null,
        fzr: null,
        safeMember: null,
        fzrPhone: null,
        safePhone: null,
        comNum: 1,
        imptArea: null,
        creater: null,
        createTime: null,
        operter: null,
        operTime: null,
        status: null
      };
      this.resetForm("form");
    },
    changeMonth(e) {
      const year = e.split('-')[0]
      const month = e.split('-')[1]
      const day = getLastDay(year, month)
      this.$set(this.formExport, 'beginDate', e + '-01')
      this.$set(this.formExport, 'endDate', e + '-' + day)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    changeQueryXqid(v) {
      this.queryParams.deptId = v[v.length - 1]
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(companyExport, {
        ...this.formExport
      }, `company_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
